package logic

import (
	"context"
	"fmt"
	"wechat/models/mysql"

	"wechat/userrpc/internal/svc"
	"wechat/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GroupAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGroupAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GroupAddLogic {
	return &GroupAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 分组添加
func (l *GroupAddLogic) GroupAdd(in *userrpc.GroupAddRequest) (*userrpc.GroupAddResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	mysql.DB.Where("id = ?", in.UserId).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在")
	}

	group := mysql.Group{
		Name:   in.Name,
		UserId: in.UserId,
	}

	mysql.DB.Debug().Create(&group)

	return &userrpc.GroupAddResponse{Id: int64(group.ID)}, nil
}
